home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Collections: MegaDisc
/
MegaDisc 28 (1992-05)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip
/
MegaDisc 28 (1992-05)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf
/
Programming
/
Basic_Booleans
/
Basic_Booleans
Wrap
Text File
|
1992-05-26
|
5KB
|
171 lines
B O O L E A N B A S I C S
by Paul McLachlan
Ed: Here's a fine tutorial on a little-used aspect of programming in
Basic, and which can add a lot of elegance and speed to your programs.
||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 |||
Booleans are facinating tools for programming in BASIC. They allow you to
get the computer to do more, with fewer lines. They are also considerably
faster. Unfortunately, these are little spoken of features of BASIC, and
you see nothing of them in the AmigaBASIC manual supplied with your
computer. They are useful in only a small variety of circumstances, but
when they do come in handy, you'll be glad you knew about them. Please
note that before we start this, all boolean statements can be replaced by
conventional BASIC statements (eg IF-THEN-ELSE). It just requires a few
more lines.
A BOOLEAN EXPRESSION is a way of asking the computer whether something is
true, or false. If the expression is true, AmigaBASIC returns a -1. If
the expression is false, AmigaBASIC returns a 0. Consider the statement:
PRINT a=b
Now, we all know that this will not output an 'a', followed by an equal
sign, followed by a 'b', because it is not enclosed in double quotes. (").
We also know it cannot output the value of the variable 'a=b', because
variables do not have equals signs in them. So, what does it do? It
evaluates the expression 'a=b' and decides if it is true, or false. If it
is true, ie, if a does in fact equal b, then the Amiga will print a -1 on
the screen. If it is false, the Amiga will print a 0. In the same way,
the following works.
c=a=b
This makes 'c' equal to the outcome of the test, 'a=b'. If a is equal to
b, then c becomes equal to -1. If a is not equal to b, then c becomes
equal to 0.
Boolean expressions must be enclosed in brackets when there are more than
one together. Brackets are also used to increase readibility with one
boolean expression. Take the first example, and compare it with the second
example.
c=a=b
c=(a=b)
Obviously the second example is easier to read, and it makes it easier to
debug. Longer boolean expressions must be enclosed in brackets. Eg
a=(b=(c=(d=5)))
Keep in mind that for a to be equal to -1, the following conditions must
be met.
if d is equal to 5:
if c is equal to -1:
b must be equal to -1
if c is equal to 0:
b must be equal to 0:
if, however, d is not equal to 5
if c is equal to -1:
b must be equal to 0
if c is equal to 0:
b must be equal to -1
So, we see, this is quite an impressive list of options. To do the same
with conventional BASIC requires this: (assuming all variables have been
set before)
REM SIMULATING THE BOOLEAN STATEMENT A=(B=(C=(D=5)))
IF D=5 THEN
IF C=-1 THEN
IF B=-1 THEN
LET A=-1
ELSE
LET A=0
END IF
ELSE
IF B=0 THEN
LET A=0
ELSE
LET A=-1
END IF
END IF
ELSE
IF C=-1 THEN
IF B=-1 THEN
LET A=0
ELSE
LET A=-1
END IF
ELSE
IF B=0 THEN
LET A=-1
ELSE
LET A=0
END IF
END IF
END IF
So, it appears that booleans just might come in handy one day, doesn't it?
Boolean expressions are not just limited to ='s. <'s,>'s, AND's, OR's,
and everything that you might usually use in an IF statement are allowed.
Be forewarned, however - don't over-do it. Some boolean expressions can be
slower than the IF-THEN equivalent. Not many of them are like this,
especially not the example above - most are quicker. However, all speed
hints when using AmigaBASIC should be passed around, so I'm just warning
you.
I'm not sure whether ALL types of BASIC on the Amiga use -1 to return a
true. I know that the Apple II Microsoft BASIC returns a 1, instead of a
-1. Some commercial BASIC's may use 1's also. It's easy to find out.
Just type
PRINT 1=1
And the correct response for true will be returned (1 or -1). Note that
this also works in the following manner.
PRINT 5=9
returns a 0
PRINT 90=90
returns a -1 (or a 1)
etc. Although I can't think of a reason to do this, you might be able to.
EXERCISES
To use these exercises, try to work out what a will equal, and then enter
the expression into the computer, and print a, to see if you were correct.
All these exercises use the values of these variables:
b=5, c=9, d=15, e=5, f=4, g=2, h=15, i=-1, j=-1, k=-1, l=0, m=0, n=-1, o=0
1)
a=(i=(b=e))
2)
a=(l=(b=f))
3)
a=(g=2*(k=(l=(h=g))))
4)
a=4*ABS(b=e)
I hope that the above is of some use to one of you.
- Paul Mclachlan
||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 ||| 28 |||